package binary.search.tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yi
 * Date: 2024-07-31
 * Time: 9:50
 */
public class BinarySearchTree {
    static class treeNode {
        public int val;
        public treeNode left;
        public treeNode right;

        public treeNode(int val) {
            this.val = val;
        }
    }

    public treeNode root = null;

    //查找节点
    public boolean search(int val) {
        treeNode cur = root;
        while (cur != null) {
            if(cur.val < val) {
                cur = cur.right;
            }else if(cur.val > val) {
                cur = cur.left;
            }else {
                return true;
            }
        }
        return false;
    }

    //插入节点
    public boolean insert(int val) {
        treeNode node = new treeNode(val);
        if(root == null) {
            root = node;
            return true;
        }
        treeNode cur = root;
        treeNode parent = null;
        while(cur != null) {
            if(val > cur.val) {
                parent = cur;
                cur = cur.right;
            }else if(val < cur.val) {
                parent = cur;
                cur = cur.left;
            }else {
                return false;
            }
        }
        if(val > parent.val) {
            parent.right = node;
        }else {
            parent.left = node;
        }
        return true;
    }

    //删除节点
    public void remove(int key) {
        treeNode cur = root;
        treeNode parent = null;
        //找到要删除的节点
        while(cur != null) {
            if(key > cur.val) {
                parent = cur;
                cur = cur.right;
            }else if(key < cur.val) {
                parent = cur;
                cur = cur.left;
            }else {
                //删除的逻辑
                removeNode(parent,cur);
                break;
            }
        }
        System.out.println("没有该节点");
    }

    //执行删除操作的方法
    private void removeNode(treeNode parent, treeNode cur) {
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            }else if(cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = cur.left;
            }else if(cur == parent.left) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            treeNode target = cur.right;
            treeNode targetParent = cur;
            while(target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(target == targetParent.right) {
                targetParent.right = target.right;
            }else {
                targetParent.left = target.right;
            }
        }
    }


}
